package com.ysq;

import java.util.Comparator;
import java.util.TreeSet;

public class TreeSetDemo {

        public static void main(String[] args) {
            test1();
            int s=sum(1,2,3,4,5);
            System.out.println(s);
        }

    //ctrl+alt+m
    private static void text0() {
        //集合智能存储引用数据类型,不能存静态数据类型
        TreeSet<Integer> set1 = new TreeSet<>();
        //1.---》包装Integer对象----》自动装箱，1自动装箱变为引用数据对象
        set1.add(5);
        set1.add(3);
        set1.add(2);
        set1.add(4);
        set1.add(6);
        set1.add(1);
        for (Integer integer : set1){
            System.out.println(integer);
        }
    }
    //c
    public static void test1() {

        TreeSet<Student> set = new TreeSet<>();
        set.add(new Student("147",19));
        set.add(new Student("ysq",20));
        set.add(new Student("shuaige",18));
        set.add(new Student("xixi",21));

        for(Student student:set ){
            System.out.println(student.getName()+"--"+student.getAge());
        }

    }
    public static void test2() {
//        Comparator<Student> comparator = new Comparator<Student>() {
//            @Override
//            public int compare(Student o1, Student o2) {
//                //如果是正序：o1-o2
//                //如果是倒序：o2-o1
//                return o1.getAge()-o2.getAge();
//            }
//        };
//        Comparator<Student> comparator = (o1, o2) -> o1.getAge()-o2.getAge();

        Comparator<Student> comparator = Comparator.comparingInt(Student::getAge);

        TreeSet<Student> set = new TreeSet<>(comparator);
        set.add(new Student("147",19));
        set.add(new Student("ysq",20));
        set.add(new Student("shuaige",18));
        set.add(new Student("xixi",21));
        for(Student student:set ){
            System.out.println(student.getName()+"--"+student.getAge());
        }
    }
    //可变参数必须是形参的最后一个
    public static  int sum(int...a){
        int total = 0;
        for(int i=0;i<a.length;i++){
            total += a[i];
        }
        return total;
    }
}
